

/*
combine auction data and govpx shocks

output:
- auction_notesbonds_govpx_shock_all.dta: 
-- notes/bonds auction data merged with shocks of all traded govpx securities
-- combined with remaining maturity
- auction_notesbonds_govpx_shock.dta: 
-- notes/bonds auction data merged with shocks of auction-specific govpx securities
*/


********************************************************************************
********************************************************************************

************************
* all govpx auction data

n di "all govpx auction shocks"
* merge maturity-remaining info with all govpx data (including govpx bills etc)
* note: some missing using data is from bonds issued before 1980
use cusip issueDate maturityDate type ///
	using ../data/td_auction_data_clean.dta, clear
* only match bills/cmbs/notes/bonds
keep if type=="Bill" | type=="CMB" | type=="Note" | type=="Bond"
drop type
* for securities with multiple issues: keep final
bys cusip maturityDate (issueDate): keep if _n==_N
merge 1:m cusip using ../data/input/govpx_shock_all.dta, nogen keep(match)

* maturity remaining (in days)
gen maturity_remaining = maturityDate - date
drop maturityDate
* time since issue (note: large negative values for re-issued)
gen maturity_lapsed = date - issueDate
drop issueDate


* merge with auction data
* note: only merge with note/bond auction dates
rename date auctionDate
rename cusip cusip_govpx
rename auction_cusip cusip
merge m:1 auctionDate cusip using ../data/td_auction_notesbonds_clean.dta, ///
	nogen keep(match)

* observations are unique on: auctionDate cusip cusip_govpx when_issued
sort auctionDate cusip maturity_remaining cusip_govpx when_issued
* save
save ../data/auction_notesbonds_govpx_shock_all.dta, replace



************************
* auction-specific govpx data
n di "auction-specific govpx shocks"
* combine auction-specific shock data with note/bonds auction data
use ../data/td_auction_notesbonds_clean.dta, clear
keep if year(auctionDate)>=1995 & year(auctionDate)<=2017
drop if missing(close_time) | missing(release_time)

rename cusip auction_cusip
rename auctionDate date

* note: 27 dates in master within timeframe and with close/release data
* but no auction-specific govpx trades in (narrow) window
merge 1:1 date auction_cusip using ../data/input/govpx_shock.dta, nogen keep(match)
rename auction_cusip cusip
rename date auctionDate

* also create quantity-based demand shocks, from bid-to-cover/accepted data
sort release_time
gen _T = _n
tsset _T

* differences and AR(4) model, after demeaning
local b2c_varlist b2c_all b2c_indirectBidder b2c_directBidder b2c_primaryDealer
local frac_varlist frac_noncompetitive frac_depository frac_individuals ///
	frac_dealers frac_pensions frac_investmentFunds ///
	frac_foreign frac_other frac_misc
foreach v of varlist `b2c_varlist' `frac_varlist' {
	egen `v'_mean = mean(`v'), by(term_group)
	gen `v'_demean = `v' - `v'_mean
	gen D_`v' = D.`v'_demean
	
	*reg `v'_demean c.( L(1/4).`v'_demean)##i.term_group
	reg `v' L(1/4).`v' i.term_group
	predict `v'_resid if e(sample), r
	drop `v'_mean `v'_demean
}
drop _T

* observations are unique on: close_time/release_time (also: auctionDate cusip )
sort release_time
save ../data/auction_notesbonds_govpx_shock.dta, replace



************************
* all govpx non-auction data
n di "on-the-run govpx non-auction shocks"

* create on-the-run dataset for non-auction dates, 1995-
* 2, 5, 10, and 30y only
use ../data/td_auction_data_clean.dta, clear
keep if type=="Note" | type=="Bond"
keep if auctionDateYear>=1995
keep if term_group==2 | term_group==5 | term_group==10 | term_group==30
* multiple re-issue of old 10-year auctions; irrelevant for on-the-run
drop if auctionDate == mdy(10,8,2008)

* on-the-run: most recently issued security (by term)
rename issueDate date
keep cusip date term_group

* fill in dates
xtset term_group date
tsfill
* fill in cusips
sort term_group date
replace cusip = cusip[_n-1] if cusip==""

* merge with non-auction govpx dates
merge 1:m cusip date using ../data/input/govpx_nonauction_shock_all.dta, ///
	nogen keep(match)
* dont use when issued for on-the-run 
drop if when_issued
drop when_issued

* observations are unique on: date term_group
sort date term_group
* save
save ../data/nonauction_notesbonds_govpx_shock.dta, replace




